﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_Dept : ATPages
{
    ZSql info = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";

        if (!IsPostBack)
        { 

            
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            if (Request["action"]=="add")
            {
                
                btnEdit.Visible = false;
            }
            if (Request["action"]=="update")
            {
                initData();
                btnAdd.Visible = false;
            }
        }
    }


  

    //添加
    protected void btnAdd_Click(object sender, EventArgs e)
    {
       
        if (txtDeptName.Text.Trim() == string.Empty)
        {

            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写部门名称')", true);
            return;
        }

        string num = info.GetScalar("select count(*) from department where deptname = '" + Common.FormatParameter(txtDeptName.Text.Trim()) + "'").ToString();

        if (num != "0")
        {

            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('存在相同的部门名称，请修改')", true);
            return;
        }

        string newid = info.GetScalar("insert into department(deptname,DepMan,DepTel,DepDescript,deptbh) values('" + Common.FormatParameter(txtDeptName.Text) + "','" + Common.FormatParameter(txtName.Text) + "','" + Common.FormatParameter(txtTel.Text) + "','" + Common.FormatParameter(txtdescript.Text) + "','"+Common.FormatParameter(txtbh.Text)+"');select nid = @@identity").ToString();

        //string newid = info.GetScalar("select max(deptid) from department").ToString();

        info.Close();


        Response.Redirect("DeptManage.aspx?moduid="+Request["moduid"]);
      
    }
    private void initData() 
    {
        ZSql zs = new ZSql();
        zs.Open("select * from Department where Deptid="+Request["id"]);
        txtDeptName.Text = zs.m_table.Rows[0]["deptname"].ToString();
        txtName.Text = zs.m_table.Rows[0]["DepMan"].ToString();
        txtTel.Text = zs.m_table.Rows[0]["DepTel"].ToString();
        txtdescript.Text = zs.m_table.Rows[0]["DepDescript"].ToString();
        txtbh.Text = zs.m_table.Rows[0]["deptbh"].ToString();
        zs.Close();
    }

    //修改
    protected void btnEdit_Click(object sender, EventArgs e)
    {


        if (txtDeptName.Text.Trim() == string.Empty)
        {

            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请填写部门名称')", true);
            return;
        }

        //string num = info.GetScalar("select count(*) from department where deptname = '" + Common.FormatParameter(txtDeptName.Text.Trim()) + "'").ToString();

        //info.Close();

        //if (num != "0")
        //{

        //    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('存在相同的部门名称，请修改')", true);
        //    return;
        //}

        info.Open("update department set deptname = '" + Common.FormatParameter(txtDeptName.Text) + "',DepMan='" + Common.FormatParameter(txtName.Text) + "',DepTel='" + Common.FormatParameter(txtTel.Text) + "',DepDescript='" + Common.FormatParameter(txtdescript.Text) + "',deptbh='"+Common.FormatParameter(txtbh.Text)+"' where deptid = " + Request["id"]);

        info.Close();


        Response.Write("<script>window.location='DeptManage.aspx?moduid=" + Request["moduid"] + "';</script>");

    }

}
